using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;

namespace WebControls
{
    public class SFListBox : ListBox, ILabel, IReadOnly, IClear, IValidation
    {
        #region ILabel Members

        private string _ControlsLabel;

        public string ControlsLabel
        {
            get
            {
                return _ControlsLabel;
            }
            set
            {
                _ControlsLabel = value;
            }
        }

        #endregion
        #region IValidation Members

        private bool _AllowNull = true;

        public bool AllowNull
        {
            get
            {
                return _AllowNull;
            }
            set
            {
                _AllowNull = value;
            }
        }
        private bool _HasErrors = false;


        public bool HasErrors
        {
            get { return _HasErrors; }
        }

        private string _ErrorMessage = "";

        public string ErrorMessage
        {
            get
            {
                return _ErrorMessage;
            }
            set
            {
                _HasErrors = (value != "");
                _ErrorMessage = value;
            }
        }

        public virtual bool Validate()
        {
            if (!AllowNull && this.Visible)
            {
                bool hasChecked = false;
                foreach (ListItem li in this.Items)
                {
                    if (li.Selected)
                    {
                        hasChecked = true;
                    }
                }
                if (!hasChecked)
                {

                    this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(16, new object[] { StaticMethods.GetErrorMessageControlName(this) });
                }
            }

            return this.HasErrors;
        }
        #endregion
        #region IClear Members

        public void Clear()
        {
            foreach (ListItem li in this.Items)
            {
                li.Selected = false;
            }
        }

        #endregion

        #region IReadOnly Members

        public bool ReadOnly
        {
            get
            {
                return !this.Enabled;
            }
            set
            {
                this.Enabled = !value;
            }
        }

        #endregion
        public void SetData(object parDataSource, string parDisplayMember, string parValueMember)
        {
            if (parDataSource is DataTable)
            {
                parDataSource = new DataView((DataTable)parDataSource);
                ((DataView)parDataSource).Sort = parDisplayMember;
            }

            this.DataSource = parDataSource;
            this.DataTextField = parDisplayMember;
            this.DataValueField = parValueMember;
            this.DataBind();


        }
    }
}
